package com.cn.kehong.service;

import static com.cn.kehong.service.OeeServiceImpl.DateRangeExample.getDateRanges;

import com.cn.kehong.api.IOeeService;
import com.cn.kehong.dao.OeeDeviceRateMapper;
import com.cn.kehong.dao.OeeMapper;
import com.cn.kehong.dao.OeeProductlineRateMapper;
import com.cn.kehong.dao.OeeWorkshopRateMapper;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class OeeServiceImpl implements IOeeService {

    @Autowired
    private OeeMapper oeeMapper;

    @Autowired
    private OeeDeviceRateMapper oeeDeviceRateMapper;
    @Autowired
    private OeeProductlineRateMapper oeeProductlineRateMapper;
    @Autowired
    private OeeWorkshopRateMapper oeeWorkshopRateMapper;

    @Override
    public List<OeeVo> getOeeOrganization(QueryForm queryForm) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int count = oeeMapper.getOeeOrganizationCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<OeeVo> list = oeeMapper.getOeeOrganization(queryForm);
        for (OeeVo oeeVo:list){
          oeeVo.setGenriDayStr(simpleDateFormat.format(oeeVo.getGenriDay()));
          if (list.size()>0){
              OeeDeviceRateExample oeeDeviceRateExample = new OeeDeviceRateExample();
              oeeDeviceRateExample.createCriteria().andOeeIdEqualTo(oeeVo.getId());
              List<OeeDeviceRate> oeeDeviceRates = oeeDeviceRateMapper.selectByExample(oeeDeviceRateExample);
              oeeVo.setOeeDeviceRates(oeeDeviceRates);

              OeeProductlineRateExample oeeProductlineRateExample = new OeeProductlineRateExample();
              oeeProductlineRateExample.createCriteria().andOeeIdEqualTo(oeeVo.getId());
              List<OeeProductlineRate> oeeProductlineRates = oeeProductlineRateMapper.selectByExample(oeeProductlineRateExample);
              oeeVo.setOeeProductlineRates(oeeProductlineRates);

              OeeWorkshopRateExample oeeWorkshopRateExample = new OeeWorkshopRateExample();
              oeeWorkshopRateExample.createCriteria().andOeeIdEqualTo(oeeVo.getId());
              List<OeeWorkshopRate> oeeWorkshopRates = oeeWorkshopRateMapper.selectByExample(oeeWorkshopRateExample);
              oeeVo.setOeeWorkshopRates(oeeWorkshopRates);
          }
        }
        if (list.size()>0){
            list.get(0).setCount(count);
        }
        return list;
    }

    @Override
    public String addOeeOrganization(Oee oee) throws Exception {
        if (null == oee){
            return "请输入oee信息";
        }
        String result = this.oeeAddChange(oee);
        if (StringUtils.isNotEmpty(result)){
            return result;
        }
        String id = CommonUtil.generateRandomNum("oee_");
        oee.setId(id);
        oee.setCreateTime(new Date());
        oee.setCreateName(oee.getCreateName());
        //新增主表oee
        int count = oeeMapper.insertSelective(oee);
        if (count < 1) {
            throw new Exception("新增主表oee失败！");
        }
        //新增oee中的设备稼动率
        if (oee.getOeeDeviceRates().size()>0){
            List<OeeDeviceRate> oeeDeviceRates = Lists.newArrayList();
            for(OeeDeviceRate oeeDeviceRate:oee.getOeeDeviceRates()){
                String oeeDeviceRateId = CommonUtil.generateRandomNum("oeeDeviceRate_");
                oeeDeviceRate.setId(oeeDeviceRateId);
                oeeDeviceRate.setOeeId(oee.getId());
                oeeDeviceRate.setCreateTime(new Date());
                oeeDeviceRates.add(oeeDeviceRate);
            }
            int ratesCount = oeeDeviceRateMapper.batchOeeDeviceRate(oeeDeviceRates);
            if (ratesCount<1){
                return "新增oee设备稼动率表失败";
            }
        }
        //新增oee产线稼动率表
        if (oee.getOeeProductlineRates().size()>0){
            List<OeeProductlineRate> oeeProductlineRates = Lists.newArrayList();
            for (OeeProductlineRate oeeProductlineRate:oee.getOeeProductlineRates()){
                String oeeProductlineRateId = CommonUtil.generateRandomNum("oeeProductlineRate_");
                oeeProductlineRate.setId(oeeProductlineRateId);
                oeeProductlineRate.setOeeId(oee.getId());
                oeeProductlineRate.setCreateTime(new Date());
                oeeProductlineRates.add(oeeProductlineRate);
                }
            int oeeProduce=oeeProductlineRateMapper.batchOeeProductlineRates(oeeProductlineRates);
            if (oeeProduce < 1) {
                throw new Exception("新增oee产线稼动率表失败！");
            }
        }
        //新增车间oee表
        if (oee.getOeeWorkshopRates().size()>0){
            List<OeeWorkshopRate> oeeWorkshopRates = Lists.newArrayList();
            for (OeeWorkshopRate oeeWorkshopRate:oee.getOeeWorkshopRates()){
                String oeeWorkshopRateId = CommonUtil.generateRandomNum("oeeWorkshopRate_");
                oeeWorkshopRate.setId(oeeWorkshopRateId);
                oeeWorkshopRate.setOeeId(oee.getId());
                oeeWorkshopRate.setCreateTime(new Date());
                oeeWorkshopRates.add(oeeWorkshopRate);
            }
            int oeeWorks= oeeWorkshopRateMapper.batchOeeWorkshopRates(oeeWorkshopRates);
            if (oeeWorks < 1) {
                throw new Exception("新增车间oee表失败！");
            }
        }

        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteOeeOrganization(QueryForm queryForm) throws Exception{
        String id = queryForm.getId();

        int count = oeeMapper.deleteByPrimaryKey(id);
        if (count < 1) {
            return  "删除主表oee失败！";
        }

         OeeDeviceRateExample oeeDeviceRateExample = new OeeDeviceRateExample();
         oeeDeviceRateExample.createCriteria().andOeeIdEqualTo(id);
         int i = oeeDeviceRateMapper.deleteByExample(oeeDeviceRateExample);
         if (i < 1) {
             return "删除设备稼动率失败！";
         }

        OeeProductlineRateExample oeeProductlineRateExample = new OeeProductlineRateExample();
        oeeProductlineRateExample.createCriteria().andOeeIdEqualTo(id);
        int i1 = oeeProductlineRateMapper.deleteByExample(oeeProductlineRateExample);
        if (i1 < 1) {
            return "删除产线稼动率失败！";
        }
        OeeWorkshopRateExample oeeWorkshopRateExample = new OeeWorkshopRateExample();
        oeeWorkshopRateExample.createCriteria().andOeeIdEqualTo(id);
        int i2 = oeeWorkshopRateMapper.deleteByExample(oeeWorkshopRateExample);
        if (i2 < 1) {
            return "删除车间oee失败！";
        }

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateOeeOrganization(Oee oee) throws Exception{
        String id = oee.getId();
        if (StringUtils.isEmpty(id)){
            return "id is null";
        }
        String result = this.oeeAddChange(oee);
        if (!"".equals(result)){
            return result;
        }
        oee.setUpdateName(oee.getCreateName());
        oee.setUpdateTime(new Date());
        OeeExample oeeExample = new OeeExample();
        oeeExample.createCriteria().andIdEqualTo(id);
        int i = oeeMapper.updateByExampleSelective(oee,oeeExample);
        if (i<1){
            throw new Exception("修改主表oee失败！");
        }
        //子表数据先删除后新增
        if (oee.getOeeDeviceRates().size()>0){
            OeeDeviceRateExample oeeDeviceRateExample = new OeeDeviceRateExample();
            oeeDeviceRateExample.createCriteria().andOeeIdEqualTo(id);
            int i1 = oeeDeviceRateMapper.deleteByExample(oeeDeviceRateExample);
            if (i1<1){
                throw new Exception("修改oee设备稼动率表失败！");
            }
//            for (OeeDeviceRate oeeDeviceRate:oee.getOeeDeviceRates()){
//                String oeeDeviceRateId = CommonUtil.generateRandomNum("oeeDeviceRate_");
//                oeeDeviceRate.setId(oeeDeviceRateId);
//                oeeDeviceRate.setOeeId(oee.getId());
//                oeeDeviceRate.setCreateTime(new Date());
//                oeeDeviceRateMapper.insertSelective(oeeDeviceRate);
//            }
            List<OeeDeviceRate> oeeDeviceRates = Lists.newArrayList();
            for(OeeDeviceRate oeeDeviceRate:oee.getOeeDeviceRates()){
                String oeeDeviceRateId = CommonUtil.generateRandomNum("oeeDeviceRate_");
                oeeDeviceRate.setId(oeeDeviceRateId);
                oeeDeviceRate.setOeeId(oee.getId());
                oeeDeviceRate.setCreateTime(new Date());
                oeeDeviceRates.add(oeeDeviceRate);
            }
            int ratesCount = oeeDeviceRateMapper.batchOeeDeviceRate(oeeDeviceRates);
            if (ratesCount<1){
                throw new Exception("新增oee设备稼动率表失败");
            }
        }
        if (oee.getOeeProductlineRates().size()>0){
            OeeProductlineRateExample oeeProductlineRateExample = new OeeProductlineRateExample();
            oeeProductlineRateExample.createCriteria().andOeeIdEqualTo(id);
            int i2 = oeeProductlineRateMapper.deleteByExample(oeeProductlineRateExample);
            if (i2<1){
                throw new Exception("修改oee产线稼动率表失败！");
            }
//            for (OeeProductlineRate oeeProductlineRate:oee.getOeeProductlineRates()){
//                    String oeeProductlineRateId = CommonUtil.generateRandomNum("oeeProductlineRate_");
//                    oeeProductlineRate.setId(oeeProductlineRateId);
//                    oeeProductlineRate.setOeeId(oee.getId());
//                    oeeProductlineRate.setCreateTime(new Date());
//                    oeeProductlineRateMapper.insertSelective(oeeProductlineRate);
//            }
            List<OeeProductlineRate> oeeProductlineRates = Lists.newArrayList();
            for (OeeProductlineRate oeeProductlineRate:oee.getOeeProductlineRates()){
                String oeeProductlineRateId = CommonUtil.generateRandomNum("oeeProductlineRate_");
                oeeProductlineRate.setId(oeeProductlineRateId);
                oeeProductlineRate.setOeeId(oee.getId());
                oeeProductlineRate.setCreateTime(new Date());
                oeeProductlineRates.add(oeeProductlineRate);
            }
            int oeeProduce=oeeProductlineRateMapper.batchOeeProductlineRates(oeeProductlineRates);
            if (oeeProduce < 1) {
                throw new Exception("新增oee产线稼动率表失败！");
            }
        }
        if (oee.getOeeWorkshopRates().size()>0){
            OeeWorkshopRateExample oeeWorkshopRateExample = new OeeWorkshopRateExample();
            oeeWorkshopRateExample.createCriteria().andOeeIdEqualTo(id);
            int i3 = oeeWorkshopRateMapper.deleteByExample(oeeWorkshopRateExample);
            if (i3<1){
                throw new Exception("修改车间oee表失败！");
            }
            List<OeeWorkshopRate> oeeWorkshopRates = Lists.newArrayList();
            for (OeeWorkshopRate oeeWorkshopRate:oee.getOeeWorkshopRates()){
                String oeeWorkshopRateId = CommonUtil.generateRandomNum("oeeWorkshopRate_");
                oeeWorkshopRate.setId(oeeWorkshopRateId);
                oeeWorkshopRate.setOeeId(oee.getId());
                oeeWorkshopRate.setCreateTime(new Date());
                oeeWorkshopRates.add(oeeWorkshopRate);
            }
            int oeeWorks= oeeWorkshopRateMapper.batchOeeWorkshopRates(oeeWorkshopRates);
            if (oeeWorks < 1) {
                throw new Exception("新增车间oee表失败！");
            }
//            for (OeeWorkshopRate oeeWorkshopRate:oee.getOeeWorkshopRates()){
//                String oeeWorkshopRateId = CommonUtil.generateRandomNum("oeeWorkshopRate_");
//                oeeWorkshopRate.setId(oeeWorkshopRateId);
//                oeeWorkshopRate.setOeeId(oee.getId());
//                oeeWorkshopRate.setCreateTime(new Date());
//                oeeWorkshopRateMapper.insertSelective(oeeWorkshopRate);
//            }
        }
        return CommonUtil.outStr(i);
    }

    @Override
    public List<OeeWorkshopRate> getOeeWeekly(QueryForm queryForm) throws Exception {
        Map<String, OeeWorkshopRate> aggregatedRates = new LinkedHashMap<>();

        List<String> dateRanges = getDateRanges(queryForm.getStartYear(), queryForm.getStartMonth(), queryForm.getEndMonth());
        int index = 1;
        for (String range : dateRanges) {
            BigDecimal oeeNumberSum = BigDecimal.ZERO;
            BigDecimal organizational = BigDecimal.ZERO;
            BigDecimal tpm = BigDecimal.ZERO;
            BigDecimal change = BigDecimal.ZERO;
            BigDecimal technology = BigDecimal.ZERO;
            BigDecimal yield = BigDecimal.ZERO;
            BigDecimal performance = BigDecimal.ZERO;
            String[] split = range.split(" - ");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = dateFormat.parse(split[0]);
            Date endTime = dateFormat.parse(split[1]);

            OeeExample oeeExample = new OeeExample();
            oeeExample.createCriteria().andFactoryWorkshopEqualTo(queryForm.getFactoryWorkshop()).andOrganizationTimeBetween(startTime, endTime);
            List<Oee> oees = oeeMapper.selectByExample(oeeExample);

            if (oees.isEmpty()) {
                OeeWorkshopRate oeeWorkshopRate = new OeeWorkshopRate();
                oeeWorkshopRate.setIndex("W" + index + " " + range);
                oeeWorkshopRate.setOeeNumber("0.0");
                oeeWorkshopRate.setOrganizationalProportion("0.0");
                oeeWorkshopRate.setTechnologyRoportion("0.0");
                oeeWorkshopRate.setTpmRoportion("0.0");
                oeeWorkshopRate.setChangeRoportion("0.0");
                oeeWorkshopRate.setYieldRoportion("0.0");
                oeeWorkshopRate.setPerformanceLoss("0.0");
                aggregatedRates.put("W" + index, oeeWorkshopRate);
            } else {
                for (Oee oee : oees) {
                    OeeWorkshopRate oeeWorkshopRate = new OeeWorkshopRate();
                    oeeWorkshopRate.setIndex("W" + index + " " + range);
                    oeeWorkshopRate.setOeeNumber("0.0");
                    oeeWorkshopRate.setOrganizationalProportion("0.0");
                    oeeWorkshopRate.setTechnologyRoportion("0.0");
                    oeeWorkshopRate.setTpmRoportion("0.0");
                    oeeWorkshopRate.setChangeRoportion("0.0");
                    oeeWorkshopRate.setYieldRoportion("0.0");
                    oeeWorkshopRate.setPerformanceLoss("0.0");
                    aggregatedRates.put("W" + index, oeeWorkshopRate);
                    OeeWorkshopRateExample rateExample = new OeeWorkshopRateExample();
                    rateExample.createCriteria().andOeeIdEqualTo(oee.getId()).andStatusEqualTo("车间综合");
                    List<OeeWorkshopRate> workshopRates = oeeWorkshopRateMapper.selectByExample(rateExample);

                    for (OeeWorkshopRate workshopRate : workshopRates) {
                        //周OEE的计算公式为：每天的节拍*对应每天的合格品数量（一周每天之和）/一周内每天负荷时间之和（负荷时间：计划工作时间-计划停机时间）
                        //基准节拍
                        BigDecimal benchmarkBeat = new BigDecimal(workshopRate.getBenchmarkBeat());
                        //合格品数量
                        BigDecimal qualifiedQuantity = workshopRate.getQualifiedQuantity();
                        BigDecimal bigInt1 = new BigDecimal(workshopRate.getPlanWorkTime());
                        BigDecimal bigInt2=  new BigDecimal(workshopRate.getPlanStopTime());
                        //负荷时间
                        BigDecimal  loadTime = bigInt1.subtract(bigInt2);
                        MathContext mc = new MathContext(2, java.math.RoundingMode.HALF_UP);
                        BigDecimal oeeDecimal = (benchmarkBeat.multiply(qualifiedQuantity)).divide(loadTime, mc);
//                        String[] oeeSplit = workshopRate.getOeeNumber().split("%");
                        String[] organSplit = workshopRate.getOrganizationalProportion().split("/");
                        String[] technologSplit = workshopRate.getTechnologyRoportion().split("/");
                        String[] tpmSplit = workshopRate.getTpmRoportion().split("/");
                        String[] changeSplit = workshopRate.getChangeRoportion().split("/");
                        String[] yieldSplit = workshopRate.getYieldRoportion().split("/");
                        String[] performanceSplit = workshopRate.getPerformanceLoss().split("/");
                        oeeNumberSum = oeeNumberSum.add(new BigDecimal(oeeDecimal.stripTrailingZeros().toPlainString()));
                        //组织损失
                        BigDecimal organs = new BigDecimal(organSplit[0]).divide(loadTime, mc);
                        organizational = organizational.add(new BigDecimal(organs.stripTrailingZeros().toPlainString()));
                        //tpm损失
                        BigDecimal tpms = new BigDecimal(tpmSplit[0]).divide(loadTime, mc);
                        tpm = tpm.add(new BigDecimal(tpms.stripTrailingZeros().toPlainString()));
                        //换型损失
                        BigDecimal changes = new BigDecimal(changeSplit[0]).divide(loadTime, mc);
                        change = change.add(new BigDecimal(changes.stripTrailingZeros().toPlainString()));
                        //技术损失
                        BigDecimal technologys = new BigDecimal(technologSplit[0]).divide(loadTime, mc);
                        technology = technology.add(new BigDecimal(technologys.stripTrailingZeros().toPlainString()));

                        //性能损失
                        BigDecimal performances = new BigDecimal(performanceSplit[0]).divide(loadTime, mc);
                        performance = performance.add(new BigDecimal(performances.stripTrailingZeros().toPlainString()));
                        //良率损失
                        BigDecimal yields = new BigDecimal(yieldSplit[0]).divide(loadTime, mc);
                        yield = yield.add(new BigDecimal(yields.stripTrailingZeros().toPlainString()));

                    }
                    OeeWorkshopRate aggregatedRate = new OeeWorkshopRate();
                    aggregatedRate.setIndex("W" + index + " " + range);
                    aggregatedRate.setOeeNumber(oeeNumberSum.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRate.setOrganizationalProportion(organizational.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRate.setTpmRoportion(tpm.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRate.setTechnologyRoportion(technology.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRate.setChangeRoportion(change.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRate.setYieldRoportion(yield.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRate.setPerformanceLoss(performance.setScale(5, RoundingMode.HALF_UP).toString());
                    aggregatedRates.put("W" + index, aggregatedRate);
                }

            }
            index++;
        }

        List<OeeWorkshopRate> list = new ArrayList<>(aggregatedRates.values());
        return list;
    }

    @Override
    public List<OeeWorkshopRate> getOeeMonth(QueryForm queryForm) throws Exception {
        Integer startYear = queryForm.getStartYear();
        Map<String, OeeWorkshopRate> aggregatedRates = new LinkedHashMap<>();
        int index = 1;
        for(int i = 1; i <= 12; i++) {
            BigDecimal oeeNumberSum = BigDecimal.ZERO;
            BigDecimal organizational = BigDecimal.ZERO;
            BigDecimal tpm = BigDecimal.ZERO;
            BigDecimal change = BigDecimal.ZERO;
            BigDecimal technology = BigDecimal.ZERO;
            BigDecimal yield = BigDecimal.ZERO;
            BigDecimal performance = BigDecimal.ZERO;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime=dateFormat.parse(startYear+"-"+i+"-"+"01");
            Date endTime=dateFormat.parse(startYear+"-"+i+"-"+"31");
            OeeExample oeeExample = new OeeExample();
            oeeExample.createCriteria().andFactoryWorkshopEqualTo(queryForm.getFactoryWorkshop()).andOrganizationTimeBetween(startTime, endTime);
            List<Oee> oees = oeeMapper.selectByExample(oeeExample);
            if (oees.isEmpty()) {
                OeeWorkshopRate oeeWorkshopRate = new OeeWorkshopRate();
                oeeWorkshopRate.setIndex("W" + index + " " + i);
                oeeWorkshopRate.setOeeNumber("0");
                oeeWorkshopRate.setOrganizationalProportion("0");
                oeeWorkshopRate.setTechnologyRoportion("0");
                oeeWorkshopRate.setTpmRoportion("0");
                oeeWorkshopRate.setChangeRoportion("0");
                oeeWorkshopRate.setYieldRoportion("0");
                oeeWorkshopRate.setPerformanceLoss("0");
                aggregatedRates.put("W" + index, oeeWorkshopRate);
            } else {
                for (Oee oee : oees) {
                    OeeWorkshopRate oeeWorkshopRate = new OeeWorkshopRate();
                    oeeWorkshopRate.setIndex("W" + index + " " + i);
                    oeeWorkshopRate.setOeeNumber("0.0");
                    oeeWorkshopRate.setOrganizationalProportion("0.0");
                    oeeWorkshopRate.setChangeRoportion("0.0");
                    oeeWorkshopRate.setYieldRoportion("0.0");
                    oeeWorkshopRate.setPerformanceLoss("0.0");
                    aggregatedRates.put("W" + index, oeeWorkshopRate);
                    OeeWorkshopRateExample rateExample = new OeeWorkshopRateExample();
                    rateExample.createCriteria().andOeeIdEqualTo(oee.getId()).andStatusEqualTo("车间综合");
                    List<OeeWorkshopRate> workshopRates = oeeWorkshopRateMapper.selectByExample(rateExample);

                    for (OeeWorkshopRate workshopRate : workshopRates) {
                        //周OEE的计算公式为：每天的节拍*对应每天的合格品数量（一周每天之和）/一周内每天负荷时间之和（负荷时间：计划工作时间-计划停机时间）
                        //基准节拍
                        BigDecimal benchmarkBeat = new BigDecimal(workshopRate.getBenchmarkBeat());
                        //合格品数量
                        BigDecimal qualifiedQuantity = workshopRate.getQualifiedQuantity();
                        BigDecimal bigInt1 = new BigDecimal(workshopRate.getPlanWorkTime());
                        BigDecimal bigInt2=  new BigDecimal(workshopRate.getPlanStopTime());
                        //负荷时间
                        BigDecimal  loadTime = bigInt1.subtract(bigInt2);
                        MathContext mc = new MathContext(2, java.math.RoundingMode.HALF_UP);
                        BigDecimal oeeDecimal = (benchmarkBeat.multiply(qualifiedQuantity)).divide(loadTime, mc);
//                        String[] oeeSplit = workshopRate.getOeeNumber().split("%");
                        String[] organSplit = workshopRate.getOrganizationalProportion().split("/");
                        String[] technologSplit = workshopRate.getTechnologyRoportion().split("/");
                        String[] tpmSplit = workshopRate.getTpmRoportion().split("/");
                        String[] changeSplit = workshopRate.getChangeRoportion().split("/");
                        String[] yieldSplit = workshopRate.getYieldRoportion().split("/");
                        String[] performanceSplit = workshopRate.getPerformanceLoss().split("/");
                        oeeNumberSum = oeeNumberSum.add(new BigDecimal(oeeDecimal.stripTrailingZeros().toPlainString()));
                        //组织损失
                        BigDecimal organs = new BigDecimal(organSplit[0]).divide(loadTime, mc);
                        organizational = organizational.add(new BigDecimal(organs.stripTrailingZeros().toPlainString()));
                        //tpm损失
                        BigDecimal tpms = new BigDecimal(tpmSplit[0]).divide(loadTime, mc);
                        tpm = tpm.add(new BigDecimal(tpms.stripTrailingZeros().toPlainString()));
                        //换型损失
                        BigDecimal changes = new BigDecimal(changeSplit[0]).divide(loadTime, mc);
                        change = change.add(new BigDecimal(changes.stripTrailingZeros().toPlainString()));
                        //技术损失
                        BigDecimal technologys = new BigDecimal(technologSplit[0]).divide(loadTime, mc);
                        technology = technology.add(new BigDecimal(technologys.stripTrailingZeros().toPlainString()));

                        //性能损失
                        BigDecimal performances = new BigDecimal(performanceSplit[0]).divide(loadTime, mc);
                        performance = performance.add(new BigDecimal(performances.stripTrailingZeros().toPlainString()));
                        //良率损失
                        BigDecimal yields = new BigDecimal(yieldSplit[0]).divide(loadTime, mc);
                        yield = yield.add(new BigDecimal(yields.stripTrailingZeros().toPlainString()));

                    }

                }
                OeeWorkshopRate aggregatedRate = new OeeWorkshopRate();
                aggregatedRate.setIndex("W" + index + " " + i);
                aggregatedRate.setOeeNumber(oeeNumberSum.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRate.setOrganizationalProportion(organizational.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRate.setTpmRoportion(tpm.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRate.setTechnologyRoportion(technology.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRate.setChangeRoportion(change.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRate.setYieldRoportion(yield.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRate.setPerformanceLoss(performance.setScale(5, RoundingMode.HALF_UP).toString());
                aggregatedRates.put("W" + index, aggregatedRate);
            }
            index++;
        }
        List<OeeWorkshopRate> list = new ArrayList<>(aggregatedRates.values());
        return list;
    }

    private String oeeAddChange(Oee oee){

        String result = "";

        if (ObjectUtils.isEmpty(oee.getGenriDay())){
            result = "请输入属日";
            return result;
        }
        if (StringUtils.isBlank(oee.getOrganizationName())){
            result = "请输入编制人";
            return result;
        }
        if (ObjectUtils.isEmpty(oee.getOrganizationTime())){
            result = "请输入编制时间";
            return result;
        }
        for (OeeProductlineRate oeeProductlineRate:oee.getOeeProductlineRates()){
            if (ObjectUtils.isEmpty(oeeProductlineRate.getMaximumOutput())){
                result = "请输入理论最大产量";
                return result;
            }
        }
        for (OeeWorkshopRate oeeWorkshopRate:oee.getOeeWorkshopRates()){
            if (StringUtils.isBlank(oeeWorkshopRate.getBenchmarkBeat())){
                result = "请输入基准节拍";
                return result;
            }
            if (StringUtils.isBlank(oeeWorkshopRate.getActualBeat())){
                result = "请输入实际节拍";
                return result;
            }
        }
        return result;

    }


    public static class DateRangeExample {

        public static void main(String[] args) {
            int startYear = 2024;
            int startMonth = 1;
            int endMonth = 2;

            List<String> dateRanges = getDateRanges(startYear, startMonth, endMonth);

            for (String range : dateRanges) {
                System.out.println(range);
            }
        }

        public static List<String> getDateRanges(int startYear, int startMonth, int endMonth) {
            List<String> dateRanges = new ArrayList<>();
            LocalDate startDate = LocalDate.of(startYear, startMonth, 1);
            LocalDate endDate = LocalDate.of(startYear, endMonth, 1).with(TemporalAdjusters.lastDayOfMonth());

            LocalDate friday = startDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
            while (friday.isBefore(endDate) || friday.isEqual(endDate)) {
                LocalDate nextThursday = friday.with(TemporalAdjusters.next(DayOfWeek.THURSDAY));
                String dateRange = formatDateRange(friday, nextThursday);
                dateRanges.add(dateRange);
                friday = nextThursday.plusDays(1).with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
            }

            return dateRanges;
        }

        public static String formatDateRange(LocalDate start, LocalDate end) {
            return start + " - " + end;
        }
    }

}
